﻿using System;
using System.Windows.Controls;
using System.Windows.Media;

namespace WickedFlame.Util.Controls
{
    /// <summary>
    /// Image type that can be selected
    /// Background changes to blue when is selected or mouse over
    /// </summary>
	public class ToolsButtonImage : Image
	{
		bool _mouseOver = false;
		bool _selected = false;

		public ToolsButtonImage()
		{
			this.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(OnMouseLeftDown);
			this.MouseEnter += new System.Windows.Input.MouseEventHandler(OnMouseEnter);
			this.MouseLeave += new System.Windows.Input.MouseEventHandler(OnMouseLeave);
		}


		public bool IsSelected
		{
			get
			{
				return _selected;
			}
			set
			{
				_selected = value;
				this.InvalidateVisual();
			}
		}

		protected override void OnRender(System.Windows.Media.DrawingContext dc)
		{
			if (IsSelected || _mouseOver)
			{
				dc.DrawRectangle(new LinearGradientBrush(Colors.LightBlue,Colors.Wheat,90),
					new Pen(Brushes.Blue, 1),
					new System.Windows.Rect(0, 0, this.Width, this.Height));
			}

			base.OnRender(dc);
		}

		private void OnMouseLeftDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			IsSelected = true;
		}

		private void OnMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
		{
			_mouseOver = true;
			this.InvalidateVisual();
		}

		private void OnMouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
		{
			_mouseOver = false;
			this.InvalidateVisual();
		}
	}
}
